from os import listdir
from PIL import Image
 
 
def pinjie():
    # 获取当前文件夹中所有JPG图像
    # im_list = [Image.open(fn) for fn in listdir(path='./merged') if fn.endswith('.jpg')]
    im_list = [Image.open(f'./merged/{fn}') for fn in listdir(path='./merged')]
 
    # 图片转化为相同的尺寸
    ims = []
    for i in im_list:
        print(i)
        new_img = i.resize((256, 256), Image.BILINEAR)
        ims.append(new_img)
 
    # 单幅图像尺寸
    width, height = ims[0].size
 
    # 创建空白长图
    # result = Image.new(ims[0].mode, (width, height * len(ims)))
    result = Image.new(ims[0].mode, (width*2, height*2))
    result.paste(ims[0], box=(0, 0))
    result.paste(ims[1], box=(width, 0))
    result.paste(ims[2], box=(0, 1 * height))
    result.paste(ims[3], box=(width, 1 * height))
 
    # 拼接图片
    # for i, im in enumerate(ims):
    #     result.paste(im, box=(0, i * height))
 
    # 保存图片
    result.save('res1.jpg')
 
 
if __name__ == '__main__':
    pinjie()
